package com.robot.merchant.controller;


import com.alibaba.fastjson.JSON;
import com.robot.merchant.pojo.Store;
import com.robot.merchant.service.StoreService;
import entity.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/store")
public class StoreJsonController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private StoreService storeService;

    /**
     * 店铺列表并分页和条件查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "third/findStores/{page}/{size}", produces = "application/json; charset=utf-8")
    public String thirdFindStores(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Store> pageList = storeService.findSearch(searchMap, page, size);
        logger.info("searchMap" + searchMap);
        logger.info("pageList" + pageList.toString());
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 店铺详情
     *
     * @param id
     * @return
     */
    @GetMapping(value = "third/findStoreById/{id}", produces = "application/json; charset=utf-8")
    public String thirdFindStoreById(@PathVariable String id) {
        return JSON.toJSONString(storeService.findById(id));
    }

    @RequestMapping(value = "third/findEnableStoreById/{id}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String thirdFindEnableStoreById(@PathVariable String id) {
        return JSON.toJSONString(storeService.findEnableStoreById(id));
    }

    /**
     * 通过phone查询店铺详情
     *
     * @param phone
     * @return
     */
    @GetMapping(value = "third/findStoreByPhone/{phone}", produces = "application/json; charset=utf-8")
    public String thirdFindStoreByPhone(@PathVariable String phone) {
        return JSON.toJSONString(storeService.findByPhone(phone));
    }

    /**
     * 通过id和companyId查询店铺详情
     *
     * @param id
     * @param companyId
     * @return
     */
    @GetMapping(value = "third/findStoreByIdAndCompanyId/{id}/{companyId}", produces = "application/json; charset=utf-8")
    public String thirdFindStoreByIdAndCompanyId(@PathVariable String id, @PathVariable String companyId) {
        return JSON.toJSONString(storeService.findByIdAndCompanyId(id, companyId));
    }

    /**
     * 通过id和agentId查询店铺详情
     *
     * @param id
     * @param agentId
     * @return
     */
    @GetMapping(value = "third/findStoreByIdAndAgentId/{id}/{agentId}", produces = "application/json; charset=utf-8")
    public String thirdFindStoreByIdAndAgentId(@PathVariable String id, @PathVariable String agentId) {
        return JSON.toJSONString(storeService.findByIdAndAgentId(id, agentId));
    }

    /**
     * 通过agentId查询店铺列表
     *
     * @param agentId
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "third/findStoresByAgentIdOrderByCreatedAtDesc/{agentId}/{page}/{size}", produces = "application/json; charset=utf-8")
    public String thirdFindStoresByAgentIdOrderByCreatedAtDesc(@PathVariable String agentId, @PathVariable int page, @PathVariable int size) {
        Page<Store> pageData = storeService.findAllByAgentIdOrderByCreatedAtDesc(agentId, page, size);
        return JSON.toJSONString(new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
    }

    /**
     * agent根据agentId查找昨天开张的店铺
     *
     * @param agentId
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "third/findStoresByAgentIdAndYesterday/{agentId}/{page}/{size}", produces = "application/json; charset=utf-8")
    public String thirdFindStoresByAgentIdAndYesterday(@PathVariable String agentId, @PathVariable int page, @PathVariable int size) {
        Page<Store> pageData = storeService.findStoresByAgentIdAndYesterday(agentId, page, size);
        return JSON.toJSONString(new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
    }

    /**
     * agent根据agentId查找最近7日开张的店铺
     *
     * @param agentId
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "third/findStoresByAgentIdAndWeek/{agentId}/{page}/{size}", produces = "application/json; charset=utf-8")
    public String thirdFindStoresByAgentIdAndWeek(@PathVariable String agentId, @PathVariable int page, @PathVariable int size) {
        Page<Store> pageData = storeService.findStoresByAgentIdAndWeek(agentId, page, size);
        return JSON.toJSONString(new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
    }

    /**
     * agent根据agentId查找总开张的店铺
     *
     * @param agentId
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "third/findStoresByAgentIdAndTotal/{agentId}/{page}/{size}", produces = "application/json; charset=utf-8")
    public String thirdFindStoresByAgentIdAndTotal(@PathVariable String agentId, @PathVariable int page, @PathVariable int size) {
        Page<Store> pageData = storeService.findStoresByAgentIdAndTotal(agentId, page, size);
        return JSON.toJSONString(new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
    }


    /**
     * 查找今日入驻商家
     *
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "third/findStoresByToday/{page}/{size}", produces = "application/json; charset=utf-8")
    public String thirdFindStoresByToday(@PathVariable int page, @PathVariable int size) {
        Page<Store> pageList = storeService.findAllByToday(page, size);
        logger.info("pageList" + pageList);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 查找昨日入驻商家
     *
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "third/findStoresByYesterday/{page}/{size}", produces = "application/json; charset=utf-8")
    public String thirdFindStoresByYesterday(@PathVariable int page, @PathVariable int size) {
        Page<Store> pageList = storeService.findAllByYesterday(page, size);
        logger.info("pageList" + pageList);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 查找最近7日入驻商家
     *
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "third/findStoresByWeek/{page}/{size}", produces = "application/json; charset=utf-8")
    public String thirdFindStoresByWeek(@PathVariable int page, @PathVariable int size) {
        Page<Store> pageList = storeService.findAllByWeek(page, size);
        logger.info("pageList" + pageList);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据时间段查找总入驻商家
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "third/findStoresByBetweenAnd/{page}/{size}", produces = "application/json; charset=utf-8")
    public String thirdFindStoresByBetweenAnd(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        logger.info("searchMap" + searchMap);
        String startTime = (String) searchMap.get("startTime") + " 00:00:00";
        String endTime = (String) searchMap.get("endTime") + " 23:59:59";
        Page<Store> pageList = storeService.findAllByBetweenAnd(startTime, endTime, page, size);
        logger.info("pageList" + pageList);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 查找总入驻商家
     *
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "third/findStoresByTotal/{page}/{size}", produces = "application/json; charset=utf-8")
    public String thirdFindStoresByTotal(@PathVariable int page, @PathVariable int size) {
        Page<Store> pageList = storeService.findAllByTotal(page, size);
        logger.info("pageList" + pageList);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }


    /**
     * 增加店铺
     *
     * @param store
     */
    @PostMapping(value = "third/addStore", produces = "application/json; charset=utf-8")
    public void thirdAddStore(@RequestBody String store) {
        storeService.add(JSON.parseObject(store, Store.class));
    }

    /**
     * 修改店铺
     *
     * @param store
     */
    @PutMapping(value = "third/updateStore")
    @Transactional(rollbackFor = Exception.class)
    public void thirdUpdateStore(@RequestBody String store) {
        storeService.update(JSON.parseObject(store, Store.class));
    }
}
